package com.cduestc.mylogin;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.pojo.Login;

public class HomePage extends AppCompatActivity{

    Button btMsg,binAccount,timeUp,commitUs,btnOutL,delAccount;
    public static String recodeEmail = "";
    public static String recodePhone = "";
    public static String recodeBirth;
    SharedPreferences share;
    SharedPreferences shareH;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_page);

        btMsg = this.findViewById(R.id.btMsg);
        binAccount =  this.findViewById(R.id.binAccount);
        btnOutL = this.findViewById(R.id.btnOutL);
        timeUp = this.findViewById(R.id.timeUp);
        commitUs = this.findViewById(R.id.commitUs);
        share = this.getSharedPreferences("myMessage",MODE_PRIVATE);
        delAccount = this.findViewById(R.id.delAccount);
        shareH = this.getSharedPreferences("selfMsg",MODE_PRIVATE);

        String uname = share.getString("username","");
        String passwd = share.getString("username","");

        if(!uname.equals("")) {
            MyLoginActivity.setSendU(uname);
        }
        if(!passwd.equals("")){
            MyLoginActivity.setSendP(passwd);
        }

        btMsg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 = new Intent(HomePage.this,MyIndex.class);
                Bundle bun = new Bundle();
                Login login = new Login(MyLoginActivity.getSendU(),MyLoginActivity.getSendP(),MyLoginActivity.recodeSex,MyLoginActivity.recodeHobby);
//                Login login1 = new Login(recodeEmail,recodeBirth,SelfMessage.recodeage,recodePhone);
                bun.putSerializable("login",login);
//                bun.putSerializable("login1",login1);
                intent1.putExtras(bun);
                startActivity(intent1);
            }
        });

        binAccount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(HomePage.this,SelfMessage.class);
                startActivityForResult(intent, 1001);
            }
        });

        delAccount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //清除数据
                SelfMessage.recodeage = 0;

                SharedPreferences.Editor seDel = shareH.edit();
                seDel.clear();
                seDel.commit();
                Toast.makeText(HomePage.this, "解除绑定成功", Toast.LENGTH_SHORT).show();
            }
        });

        //计时器
        timeUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(HomePage.this,TheTimer.class);
                startActivity(intent);
            }
        });

        //评价
        commitUs.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(HomePage.this,Evaluation.class);
                startActivity(intent);
            }
        });

        //退出
        btnOutL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(HomePage.this,AlertDialog.THEME_HOLO_LIGHT);

                builder.setIcon(android.R.drawable.stat_sys_warning).setTitle("提示消息").setMessage("确认退出吗？");

                builder.setPositiveButton("是",new DialogInterface.OnClickListener(){

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        // TODO Auto-generated method stub
                        SharedPreferences.Editor seOut = share.edit();
                        seOut.clear();
                        seOut.commit();
                        Intent intent = new Intent(HomePage.this,MyLoginActivity.class);
                        startActivity(intent);
                        Toast.makeText(HomePage.this, "退出成功", Toast.LENGTH_SHORT).show();
                    }});

                builder.setNegativeButton("否",new DialogInterface.OnClickListener(){

                    @Override
                    public void onClick(DialogInterface dialog2, int arg1) {
                        // TODO Auto-generated method stub
                        dialog2.dismiss();
                    }});
                builder.create().show();
            }
        });
    }

    //返回的信息
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, intent);
        if(requestCode == 1001 && resultCode == 2001){
            Bundle bun = intent.getExtras();
            String getEmail = bun.getString("email");
            String getBirth = bun.getString("birthday");
            String getPhone = bun.getString("phone");
            recodeEmail = getEmail;
            recodeBirth = getBirth;
            recodePhone = getPhone;
            //etpwd.setText(password);

        }
    }
}